<template>
  <div class="k-menu">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-button
      type="primary"
      style="margin-top: 10px; margin-bottom: 10px"
      @click="$router.push('/role/add')"
      >添加</el-button
    >
    <el-table :data="roleList" style="width: 100%" stripe border row-key="id">
      <el-table-column prop="id" label="编号" width="180"> </el-table-column>
      <el-table-column prop="rolename" label="名称" width="180">
      </el-table-column>

      <el-table-column prop="status" label="状态" width="180">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status == 1 ? 'primary' : 'danger'">
            {{ scope.row.status == 1 ? "启用" : "禁用" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="primary" @click="edit(scope.row.id)">编辑</el-button>
          <el-button
            type="danger"
            @click="
              dialogVisible = true;
              delId = scope.row.id;
            "
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>是否确定删除？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteRole">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
//import { mapActions } from "vuex";
export default {
  name: "k-menu",
  data() {
    return {
      roleList: [],
      dialogVisible: false, // 控制对话框是否显示
      delId: "",
    };
  },
  mounted() {
    this.getRoleList();
  },
  methods: {
    getRoleList() {
      this.$axios.get("/api/rolelist")
        .then((result) => {
          console.log(result);
          this.roleList = result.data.list;
        })
        .catch((err) => {
          console.log(err);
        });
    },

    // 角色编辑
    edit(id) {
      console.log(id);
      this.$router.push("/role/" + id);
    },

    // 删除菜单
    deleteRole() {
      console.log("确定删除么？", this.delId);
      this.dialogVisible = false;
      this.$axios.post("/api/roledelete", { id: this.delId })
        .then((result) => {
          console.log(result);
          if (result.data.code === 200) {
            this.roleList = result.data.list;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style>
</style>